package com.ruoyi.his.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.his.domain.HisPatient;
import com.ruoyi.his.service.IHisPatientService;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.his.mapper.NewHisRegistrationMapper;
import com.ruoyi.his.domain.NewHisRegistration;
import com.ruoyi.his.service.INewHisRegistrationService;

/**
 * newregistrationService业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-25
 */
@Service
public class NewHisRegistrationServiceImpl implements INewHisRegistrationService 
{
    @Autowired
    private NewHisRegistrationMapper newHisRegistrationMapper;
    @Autowired
    private IHisPatientService hisPatientService;

    /**
     * 查询newregistration
     * 
     * @param registrationId newregistration主键
     * @return newregistration
     */
    @Override
    public NewHisRegistration selectNewHisRegistrationByRegistrationId(Long registrationId)
    {
        return newHisRegistrationMapper.selectNewHisRegistrationByRegistrationId(registrationId);
    }

    /**
     * 查询newregistration列表
     * 
     * @param newHisRegistration newregistration
     * @return newregistration
     */
    @Override
    public List<NewHisRegistration> selectNewHisRegistrationList(NewHisRegistration newHisRegistration)
    {
        return newHisRegistrationMapper.selectNewHisRegistrationList(newHisRegistration);
    }

    /**
     * 新增newregistration
     * 
     * @param newHisRegistration newregistration
     * @return 结果
     */
    @Override
    public int insertNewHisRegistration(NewHisRegistration newHisRegistration)
    {
        int existingCount = newHisRegistrationMapper.checkDuplicateRegistration(newHisRegistration);
        if(existingCount>0){
            throw new ServiceException("该患者当天已存在有效挂号记录，不可重复挂号");
        }
        int queue = getQueue(newHisRegistration);
        newHisRegistration.setRegistrationQueue(String.valueOf(queue+1));
        newHisRegistration.setUpdateTime(DateUtils.getNowDate());
        newHisRegistration.setCreateTime(DateUtils.getNowDate());
        newHisRegistration.setRegistrationStatus("1");
        return newHisRegistrationMapper.insertNewHisRegistration(newHisRegistration);
    }

    /**
     * 修改newregistration
     * 
     * @param newHisRegistration newregistration
     * @return 结果
     */
    @Override
    public int updateNewHisRegistration(NewHisRegistration newHisRegistration)
    {
        newHisRegistration.setUpdateTime(DateUtils.getNowDate());
        return newHisRegistrationMapper.updateNewHisRegistration(newHisRegistration);
    }

    /**
     * 批量删除newregistration
     * 
     * @param registrationIds 需要删除的newregistration主键
     * @return 结果
     */
    @Override
    public int deleteNewHisRegistrationByRegistrationIds(Long[] registrationIds)
    {
        return newHisRegistrationMapper.deleteNewHisRegistrationByRegistrationIds(registrationIds);
    }

    /**
     * 删除newregistration信息
     * 
     * @param registrationId newregistration主键
     * @return 结果
     */
    @Override
    public int deleteNewHisRegistrationByRegistrationId(Long registrationId)
    {
        return newHisRegistrationMapper.deleteNewHisRegistrationByRegistrationId(registrationId);
    }
    /**
     * 计算挂号队列号
     */
    public  int getQueue(NewHisRegistration newHisRegistration)
    {
        return newHisRegistrationMapper.getQueue(newHisRegistration);
    }
    /**
     * 新开就诊挂号患者信息
     */
    @Override
    public HisPatient newone(NewHisRegistration newHisRegistration) {
        NewHisRegistration one = newHisRegistrationMapper.getone(newHisRegistration);
        HisPatient byPatientId = null;
        if (one != null) {
            Long registrationPatientId = one.getRegistrationPatientId();
            byPatientId = hisPatientService.selectHisPatientByPatientId(registrationPatientId);
            byPatientId.setQueueId(one.getRegistrationId());
            if (one.getTreatId()!=null){
                byPatientId.setTreatId(one.getTreatId());
            }
        }
        return byPatientId;
    }

    @Override
    public List<NewHisRegistration> getListByPatientId(Long[] patientId) {
        return newHisRegistrationMapper.getListByPatientId(patientId);
    }
}
